package com.wc.codeforces.思维.Matrix_Transformation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/4 18:52
 * @description
 * https://codeforces.com/contest/2043/problem/E
 */
public class Main {
    /**
     * 思路：
     * a -> b, 观察是 & | 运算符
     * 当 a 的位 0, b 的位 1, 那一定只能 | 运算才能转化, 也就是列运算
     * 当 a 的位 1, b 的位 0, 那一定只能 & 运算才能转化, 也就是行运算
     * 如果相等 说明不需要修改
     * 但是有一点, 在进行 列运算前, 必须将所有的行运算结束后, 才能进行列运算, 保证了列的更改, 并且不影响行运算
     * 进行行运算同理也是
     * 这样我们就可以进行图的结算, x -> y, 表示进行 x运算前先得进行 y运算, 如果遍历图中没有循环就说明是可以转化的
     * 检查循环学习到的方法, 三色法查循环法
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, M = 1010;
    static int[] h = new int[N << 1], e = new int[N << 1], ne = new int[N << 1];
    // 三色查循环法, st[i] = 1, 表示当前点正在被访问, st[i] = 2, 并且已经访问完毕, st[i] = 0, 当前点还未被访问
    static int[] st = new int[N << 1];
    // row[i] 代表改行必须修改, col[i] 表示该列必须修改
    static boolean[] row = new boolean[N], col = new boolean[N];
    static int[][] a = new int[N][M], b = new int[N][M];
    static int n, m, idx;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) a[i][j] = sc.nextInt();
            }
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) b[i][j] = sc.nextInt();
            }

            boolean success = true;
            for (int i = 0; i < 30; i++) {
                if (!check(i)) {
                    success = false;
                    break;
                }
            }

            if (success) out.println("YES");
            else out.println("NO");
        }
        out.flush();
    }

    static boolean check(int k) {
        Arrays.fill(h, 1, n + m + 1, 0);
        Arrays.fill(st, 1, n + m + 1, 0);
        Arrays.fill(row, 1, n + 1, false);
        Arrays.fill(col, 1, m + 1, false);
        idx = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (dig(a[i][j], k) != dig(b[i][j], k)) {
                    if (dig(a[i][j], k) == 1) row[i] = true;
                    else col[j] = true;
                }
                if (dig(b[i][j], k) == 0) add(j + n, i);
                else add(i, j + n);
            }
        }

        for (int i = 1; i <= n; i++) {
            if (row[i] && dfs(i)) return false;
        }

        for (int i = 1; i <= m; i++) {
            if (col[i] && dfs(i + n)) return false;
        }
        return true;
    }
    static int dig(int x, int k) {
        return x >> k & 1;
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
    // 检查是否有循环
    static boolean dfs(int x) {
        // 重复访问了, 之前已经访问过了
        if (st[x] != 0) return false;
        boolean res = false;
        st[x] = 1;
        for (int i = h[x]; i > 0; i = ne[i]) {
            int j = e[i];
            if (st[j] == 2) continue;
            if (st[j] == 0) res |= dfs(j);
            else res = true;
        }
        st[x] = 2;
        return res;
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
